<?php
/**
 * GioCMS
 * 
 * LICENSE
 *
 * This source file is subject to the GNU GENERAL PUBLIC LICENSE Version 2 
 * that is bundled with this package in the file LICENSE.txt.
 * It is also available through the world-wide-web at this URL:
 * http://www.gnu.org/licenses/gpl-2.0.txt
 * If you did not receive a copy of the license and are unable to
 * obtain it through the world-wide-web, please send an email
 * to license@ninhgio.com so we can send you a copy immediately.
 * 
 * @copyright	Copyright (c) 2010-2011 GioCMS (http://cms.ninhgio.com)
 * @license		http://www.gnu.org/licenses/gpl-2.0.txt GNU GENERAL PUBLIC LICENSE Version 2
 * @author		NinhGio - ninhgio@gmail.com
 * @since		1.0
 * @version		$Id:$
 */
?>
<?php if (isset($this->widgets) && $this->widgets) : ?>
<?php foreach ($this->widgets as $index => $widget) : ?>
<?php if ($index > 0 && $index % 3 == 0) : ?>
<div class="clear"></div>
<?php endif; ?>
<div class="col w33" id="gCoreWidget_<?php echo $widget['module_id']; ?>_<?php echo $widget['widget_id']; ?>">
	<div class="content">
		<div class="box header mb0">
			<div class="head"><div></div></div>
			<h2 class="g_a_breadcump">
				<a class="fl title" href="javascript: void(0);"><?php echo $this->TRANSLATOR->widget($widget['title'], $widget['module_id'], $widget['widget_id']); ?></a>
				<div class="clear"></div>
			</h2>
		</div>
	</div>
	<div class="content">
		<div class="desc">
			<div class="body">
				<div class="g_a_widgets_thumbnail">
				<?php if (!$widget['thumbnail']) : ?>
				<img title="<?php echo $this->TRANSLATOR->widget($widget['description'], $widget['module_id'], $widget['widget_id']); ?>" src="<?php echo sprintf('%s/modules/%s/widgets/%s/thumbnail.png', $this->APP_STATIC_SERVER, $widget['module_id'], $widget['widget_id']); ?>" />
				<?php endif; ?>
				</div>
				<div class="clear g_a_widgets_description">
					<ul>
						<li class="description"><?php echo $this->TRANSLATOR->widget($widget['description'], $widget['module_id'], $widget['widget_id']); ?></li>
						<?php if ($widget['author']) : ?>
						<li><?php echo $this->TRANSLATOR->translator('common_author', 'core'); ?>: <b><?php echo $widget['author']; ?></b></li>
						<?php endif; ?>
						<?php if ($widget['email']) : ?>
						<li><?php echo $this->TRANSLATOR->translator('common_email', 'core'); ?>: <b><?php echo $widget['email']; ?></b></li>
						<?php endif; ?>
					</ul>
				</div>
				<div class="clear fr buttons_demo">
					<?php $temp = $widget['module_id'] . '_' . $widget['widget_id']; ?>
					<?php if (in_array($temp, $this->installedWidgets)) : ?>
					<a class="button red g_a_widgets_actions" rel="uninstall_<?php echo $widget['module_id']; ?>_<?php echo $widget['widget_id']; ?>" href="javascript: void(0);"><small class="icon cross"></small><span><?php echo $this->TRANSLATOR->translator('common_button_uninstall', 'core'); ?></span></a>
					<?php else : ?>
					<a class="button green g_a_widgets_actions" rel="install_<?php echo $widget['module_id']; ?>_<?php echo $widget['widget_id']; ?>" href="javascript: void(0);"><small class="icon check"></small><span><?php echo $this->TRANSLATOR->translator('common_button_install', 'core'); ?></span></a>
					<?php endif; ?>
				</div>
				<div class="clear"></div>
			</div>
		</div>
		<div class="body_footer">
			<div class="bottom_left"><div class="bottom_right"></div></div>
		</div>
	</div>
</div>
<?php endforeach; ?>
<script type="text/javascript">
'Gio.Core.Widget'.namespace();
Gio.Core.Widget.currenAction = null;
Gio.Core.Widget.actions = function(params) {
	/**
	 * Show g_a_ajax_loading element
	 */
	$('#g_a_ajax_loading').show();
	$.ajaxq('core_widget_action', {
		type: 'post',
		url: params.url,
		data: params,
		success: function(response) {
			if (response == 'RESULT_OK') {
				switch (params.act) {
					case 'install':
						var rel = 'uninstall_' + params.module_id  + '_' + params.widget_id;
						var message = '<?php echo $this->STRING->escape($this->TRANSLATOR->translator('widget_actions_install_success')); ?>';
						var text = '<?php echo $this->STRING->escape($this->TRANSLATOR->translator('common_button_uninstall')); ?>';
						var className = 'cross';
						var color = 'red';
						break;
					case 'uninstall':
						var rel = 'install_' + params.module_id  + '_' + params.widget_id;
						var message = '<?php echo $this->STRING->escape($this->TRANSLATOR->translator('widget_actions_uninstall_success')); ?>';
						var text = '<?php echo $this->STRING->escape($this->TRANSLATOR->translator('common_button_install')); ?>';
						var className = 'check';
						var color = 'green';
						break;
				}
				$(Gio.Core.Widget.currenAction).attr('rel', rel)
								 .removeClass('red')
								 .removeClass('green')
								 .addClass(color);
				$(Gio.Core.Widget.currenAction).children('small:first')
								 .removeClass('cross')
								 .removeClass('check')
								 .addClass(className);
				$(Gio.Core.Widget.currenAction).children('span:first').html(text);
				
				Gio.Message.success(message);
			} else {
				Gio.Message.error('<?php echo $this->STRING->escape($this->TRANSLATOR->translator('common_error_message')); ?>');
			}
			/**
			 * Hide g_a_ajax_loading element
			 */
			$('#g_a_ajax_loading').hide();
		}
	});
};

$(document).ready(function() {
	$('.g_a_widgets_actions').each(function() {
		var self = this;
		$(self).bind('click', function() {
			Gio.Core.Widget.currenAction = this;
			var stringId = $(this).attr('rel');
			var temp = stringId.split('_');
			
			var act = temp[0];
			var module_id = temp[1];
			var widget_id = temp[2];
			var url = ''; 
			var container = $('#gCoreWidget_' + module_id + '_' + widget_id);
			switch (act) {
				case 'install':
					url = '<?php echo $this->url('core_widget_install'); ?>';
					var messsage_confirm = '<?php echo $this->STRING->escape($this->TRANSLATOR->translator('widget_actions_install_confirm')); ?>';
					data = { url: url, module_id: module_id, widget_id: widget_id, act: act, title: $(container).find('.title:first').html(), description: $(container).find('.description:first').html(), };
					break;
				case 'uninstall':
					url = '<?php echo $this->url('core_widget_uninstall'); ?>';
					var messsage_confirm = '<?php echo $this->STRING->escape($this->TRANSLATOR->translator('widget_actions_uninstall_confirm')); ?>';
					data = { url: url, module_id: module_id, widget_id: widget_id, act: act };
					break;
			}
			Gio.Message.yesButton = '<?php echo $this->STRING->escape($this->TRANSLATOR->translator('common_yes_button', 'core')); ?>';
			Gio.Message.noButton = '<?php echo $this->STRING->escape($this->TRANSLATOR->translator('common_no_button', 'core')); ?>';
			Gio.Message.confirm(messsage_confirm, 'Gio.Core.Widget.actions', data);
		});
	});	
});
</script>
<?php else : ?>
<p><b><?php echo $this->TRANSLATOR->translator('widget_actions_list_data_notfound'); ?></b></p>
<?php endif; ?>
<div class="clear"></div>